<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
      <el-form :inline="true" :model="filters" :size="size" ref="filters">
        <el-form-item>
          <el-select v-model="filters.shopid" placeholder="所属店铺" style="width:215px">
            <el-option v-for="item in shopList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="filters.startTime"
            type="daterange"
            range-separator="至"
            start-placeholder="开始时间"
            value-format="yyyy-MM-dd"
            @change="startTime"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="filters.endTime"
            type="daterange"
            range-separator="至"
            value-format="yyyy-MM-dd"
            start-placeholder="结束时间"
            @change="endTime"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model.number="filters.startMoney"
            auto-complete="off"
            type="age"
            placeholder="请输入所需金额"
          ></el-input>
        </el-form-item>-
        <el-form-item>
          <el-input
            v-model.number="filters.endMoney"
            auto-complete="off"
            type="age"
            placeholder="请输入所需金额"
          ></el-input>
        </el-form-item>

        <el-row>
          <el-form-item>
            <el-select v-model="filters.status" placeholder="状态" style="width:215px">
              <el-option
                v-for="item in statusList"
                :key="item.val"
                :label="item.name"
                :value="item.val"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.number="filters.startIntegral"
              auto-complete="off"
              type="age"
              placeholder="请输入所需积分"
              style="width:165px"
            ></el-input>
          </el-form-item>-
          <el-form-item>
            <el-input
              v-model.number="filters.endIntegral"
              auto-complete="off"
              type="age"
              placeholder="请输入所需积分"
              style="width:165px"
            ></el-input>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item>
            <kt-button
              icon="fa fa-search"
              :label="$t('action.search')"
              type="primary"
              @click="findPage(null)"
            />
          </el-form-item>
          <el-form-item>
            <kt-button icon="fa fa-refresh" :label="$t('action.reset')" type="primary" @click="resetForm('filters')" />
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div class="toolbar" style="float:right;padding-top:10px;padding-right:15px;">
      <el-form :inline="true" :size="size">
        <el-form-item>
          <el-button-group>
            <el-tooltip content="刷新" placement="top">
              <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
            </el-tooltip>
            <el-tooltip content="列显示" placement="top">
              <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
            </el-tooltip>
            <el-tooltip content="导出" placement="top">
              <el-button icon="fa fa-file-excel-o" @click="downloadExcel"></el-button>
            </el-tooltip>
          </el-button-group>
        </el-form-item>
      </el-form>
      <!--表格显示列界面-->
      <table-column-filter-dialog
        ref="tableColumnFilterDialog"
        :columns="columns"
        @handleFilterColumns="handleFilterColumns"
      ></table-column-filter-dialog>
    </div>
    <!--表格内容栏-->
    <cy-table
      :height="350"
      :data="pageResult"
      :columns="filterColumns"
      @findPage="findPage"
      :showOperation="true"
      :showEditOperation="false"
      :showBatchDelete="true"
      @handleDelete="handleDelete"
      ref="CyTable"
    ></cy-table>
  </div>
</template>

<script>
import CyTable from "@/views/Core/CyTable";
import KtButton from "@/views/Core/KtButton";
import { format } from "@/utils/datetime";
import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
export default {
  components: {
    CyTable,
    KtButton,
    TableColumnFilterDialog
  },
  data() {
    return {
      size: "small",
      shopList: [],
      statusList: [
        {
          val: "0",
          name: "失效"
        },
        {
          val: "1",
          name: "有效"
        }
      ],
      filters: {
        startTime: "",
        endTime: "",
        t: "marketSpecialProduct",
        shopid: "",
        startMoney: "",
        endMoney: "",
        startIntegral: "",
        endIntegral: "",
        status: ""
      },
      columns: [
        { prop: "specialp_voucher_number", label: "兑换码", minWidth: 120 },
        { prop: "shopname", label: "所属商铺", minWidth: 100 },
        { prop: "username", label: "用户名", minWidth: 80 },
        { prop: "name", label: "商品名称", minWidth: 80 },
        { prop: "money", label: "所需金额", minWidth: 80 },
        { prop: "integral", label: "所需积分", minWidth: 80 },
        { prop: "start", label: "开始时间", minWidth: 80 },
        { prop: "end", label: "结束时间", minWidth: 80 },
        { prop: "status", label: "状态", minWidth: 80 },
        { prop: "status1", label: "是否有效", minWidth: 80 },
        { prop: "create_time", label: "创建时间", minWidth: 80 },
        { prop: "creatorname", label: "创建人", minWidth: 80 },
        { prop: "update_time", label: "修改时间", minWidth: 80 },
        { prop: "updatorname", label: "修改人", minWidth: 80 }
      ],
      filterColumns: [],
      pageRequest: { pageNum: 1, pageSize: 10 },
      pageResult: {},

      operation: false, // true:新增, false:编辑
      editDialogVisible: false, // 新增编辑界面是否显示
      editLoading: false
    };
  },
  methods: {
    // 获取分页数据
    findPage: function(data) {
      this.$refs.CyTable.findPage(this.filters);
    },

    // 时间格式化
    dateFormat: function(row, column, cellValue, index) {
      return format(row[column.property]);
    },

    resetForm(formName) {
      this.filters = JSON.parse(JSON.stringify(this.defaultFilters));
      this.findPage();
      // this.$refs["filters"].resetFields();
      // if (this.$refs[formName] != undefined) {
      //   this.$refs[formName].resetFields();
      //   this.findPage();
      // }
    },

    // 处理表格列过滤显示
    displayFilterColumnsDialog: function() {
      this.$refs.tableColumnFilterDialog.setDialogVisible(true);
    },
    // 处理表格列过滤显示
    handleFilterColumns: function(data) {
      this.filterColumns = data.filterColumns;
      this.$refs.tableColumnFilterDialog.setDialogVisible(false);
    },
    // 处理表格列过滤显示
    initColumns: function() {
      var temp = [];

      $.each(this.columns, function(key, val) {
        temp.push(val);
      });
      this.filterColumns = temp;
    },
    //列表下载
    downloadExcel() {
      this.$confirm("确定下载列表文件?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let filename = "系统参数下载";
          this.$refs.CyTable.exportData(
            this.filters,
            this.filterColumns,
            filename
          );
        })
        .catch(() => {});
    },
    handleDelete: function(data) {
      let this_ = this;
      console.log(data.params);
      let paramsList = data.params;
      let idList = [];
      for (let index in paramsList) {
        idList.push(paramsList[index].id);
      }
      let params = {
        ids: idList.join(","),
        t: "marketSpecialProduct"
      };
      // params.t = "marketSpecialProduct";
      this.utils.request.deleteUserInfo(params, function(res) {
        if (res.code == "0000") {
          this_.$message({ message: "操作成功", type: "success" });
          this_.findPage(null);
        } else {
          this_.$message({ message: "操作失败, " + res.msg, type: "error" });
        }
      });
    },
    //查询所有店铺
    queryShop() {
      let that = this;
      this.utils.request.queryShop({}, res => {
        that.shopList = res.data;
      });
    },
    startTime(e) {
      console.log(e);
      this.filters.startDate = e[0];
      this.filters.endDate = e[1];
    },
    endTime(e) {
      console.log(e);
      this.filters.startTime = e[0];
      this.filters.endTime = e[1];
    }
  },
  mounted() {
    this.queryShop();
    this.initColumns();
    this.defaultFilters = JSON.parse(JSON.stringify(this.filters));
  }
};
</script>

<style scoped>
</style>
